package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.ejia.pojo.car;
import com.ejia.pojo.user;
import com.ejia.server.CarServices;
import com.ejia.server.MoneyService;
import com.ejia.server.UserService;

import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.Toolkit;

public class CLGL extends JFrame {

	private JPanel contentPane;
	private static JTable table;
	private JTextField txtname;
	private JTextField txtno;
	private JTextField txtnumber;
	private JTextField txtstatu;
	private static ArrayList<car> cars;
	private static ArrayList<user> users;
	public static int index;
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CLGL frame = new CLGL();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	
	
	public static void dataBind() {
		cars = CarServices.find();
		Object[][] data =new Object[cars.size()][];
		for (int i = 0; i < data.length; i++) {
			data[i]=new Object[] {
					cars.get(i).getCarno(),
					cars.get(i).getCarnumber(),
					cars.get(i).getUserid().getRealname(),
					cars.get(i).getUserid().getHid().getHname(),
					cars.get(i).getStatu()
			};
		}
		table.setModel(new DefaultTableModel(
				data,
				new String[] {
					"\u8F66\u724C\u53F7", "\u8F66\u4F4D\u53F7", "\u4E1A\u4E3B\u59D3\u540D", "\u5C0F\u533A\u59D3\u540D", "\u72B6\u6001"
				}
			));
		table.setFont(new Font("宋体", 0, 22));
		table.setRowHeight(30);
	}
	
	
	/**
	 * Create the frame.
	 */
	public CLGL() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(CLGL.class.getResource("/com/ejia/photo/客户管理系统.png")));
		setTitle("业主车辆管理");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1446, 715);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton button = new JButton("编辑车辆信息");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRowCount()>0) {
					index= table.getSelectedRow();
					new carBJ().setVisible(true);
					dispose();
				}else {
					JOptionPane.showMessageDialog(null, "请选择用户");
				}
				
			
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 24));
		button.setIcon(new ImageIcon(CLGL.class.getResource("/com/ejia/photo/编辑.png")));
		button.setBounds(186, 15, 260, 78);
		contentPane.add(button);
		
		JButton button_1 = new JButton("删除车辆信息");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if (table.getSelectedRowCount()>0) {
					index= table.getSelectedRow();
					CarServices.delete(cars.get(index).getCarno());
					dataBind();
				}else {
					JOptionPane.showMessageDialog(null, "请选择用户");
				}
			
			}
		});
		button_1.setIcon(new ImageIcon(CLGL.class.getResource("/com/ejia/photo/删除.png")));
		button_1.setFont(new Font("宋体", Font.PLAIN, 24));
		button_1.setBounds(461, 15, 251, 78);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("刷新车辆信息");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dataBind();
			}
		});
		button_2.setIcon(new ImageIcon(CLGL.class.getResource("/com/ejia/photo/刷新.png")));
		button_2.setFont(new Font("宋体", Font.PLAIN, 24));
		button_2.setBounds(727, 15, 257, 78);
		contentPane.add(button_2);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(15, 108, 969, 536);
		contentPane.add(scrollPane);
		
		table = new JTable();
		
		scrollPane.setViewportView(table);
		dataBind();
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u4E1A\u4E3B\u8F66\u8F86\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(999, 15, 410, 629);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("业主姓名");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 24));
		lblNewLabel.setBounds(40, 51, 117, 21);
		panel.add(lblNewLabel);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setFont(new Font("宋体", Font.PLAIN, 24));
		users=UserService.YZZHXXfind();
		Object[][] datauser= new Object[users.size()][];
		for (int i = 0; i < datauser.length; i++) {
			datauser[i]=new Object[] {
				users.get(i).getRealname()
			};
		}
		String[] datarealname = new String[users.size()];
		for (int i = 0; i < datauser.length; i++) {
			datarealname[i]=(String) datauser[i][0];
		}
		comboBox.setModel(new DefaultComboBoxModel(datarealname));
		comboBox.setBounds(172, 48, 189, 27);
		panel.add(comboBox);
		
//		txtname = new JTextField();
//		txtname.setFont(new Font("宋体", Font.PLAIN, 24));
//		txtname.setBounds(172, 51, 189, 27);
//		panel.add(txtname);
//		txtname.setColumns(10);
		
		JLabel label = new JLabel("车牌号");
		label.setFont(new Font("宋体", Font.PLAIN, 24));
		label.setBounds(40, 123, 81, 21);
		panel.add(label);
		
		txtno = new JTextField();
		txtno.setFont(new Font("宋体", Font.PLAIN, 24));
		txtno.setColumns(10);
		txtno.setBounds(172, 123, 189, 27);
		panel.add(txtno);
		
		JLabel label_1 = new JLabel("车位");
		label_1.setFont(new Font("宋体", Font.PLAIN, 24));
		label_1.setBounds(40, 195, 81, 21);
		panel.add(label_1);
		
		txtnumber = new JTextField();
		txtnumber.setFont(new Font("宋体", Font.PLAIN, 24));
		txtnumber.setColumns(10);
		txtnumber.setBounds(172, 195, 189, 27);
		panel.add(txtnumber);
		
		JLabel label_2 = new JLabel("状态");
		label_2.setFont(new Font("宋体", Font.PLAIN, 24));
		label_2.setBounds(40, 267, 81, 21);
		panel.add(label_2);
		
		txtstatu = new JTextField();
		txtstatu.setFont(new Font("宋体", Font.PLAIN, 24));
		txtstatu.setColumns(10);
		txtstatu.setBounds(172, 264, 189, 27);
		panel.add(txtstatu);
		
		JLabel label_3 = new JLabel("照片");
		label_3.setFont(new Font("宋体", Font.PLAIN, 24));
		label_3.setBounds(40, 339, 69, 21);
		panel.add(label_3);
		
		JButton button_3 = new JButton("保存");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name=(String) comboBox.getSelectedItem();
				String no=txtno.getText();
				String number=txtnumber.getText();
				String statu=txtstatu.getText();
				if (name.equals("")||no.equals("")||number.equals("")||statu.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入完整数据");
				}else {
					users=UserService.findByYZXXGL1(name);
				
				if (CarServices.add(users.get(0).getId(), no, number, statu)) {
					JOptionPane.showMessageDialog(null, "添加成功");
					dataBind();
				}else {
					JOptionPane.showMessageDialog(null, "添加失败");
				}
				}
				
			}
		});
		button_3.setBounds(272, 585, 123, 29);
		panel.add(button_3);
		
		JLabel label_4 = new JLabel("");
		label_4.setIcon(new ImageIcon(CLGL.class.getResource("/com/ejia/photo/su7_4_1_clear.png")));
		label_4.setBounds(50, 411, 326, 161);
		panel.add(label_4);
		
		JButton button_4 = new JButton("返回");
		button_4.setIcon(new ImageIcon(CLGL.class.getResource("/com/ejia/photo/返回 (1).png")));
		button_4.setFont(new Font("宋体", Font.PLAIN, 24));
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new Main().setVisible(true);
				dispose();
			}
		});
		button_4.setBounds(15, 15, 156, 78);
		contentPane.add(button_4);
		setLocationRelativeTo(null);
	}
}
